/*
 * Android Runner is a multiplayer GPS game fully written by "Xurxo Mendez Perez"
 * 
 * Copyright (C) 2009 "Xurxo Mendez Perez"
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.androidrunner.view.web.pages.game.find.list.finished;

import java.util.Iterator;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

import es.sonxurxo.androidrunner.model.persistence.util.exceptions.InstanceNotFoundException;
import es.sonxurxo.androidrunner.model.service.game.to.FinishedGameTO;
import es.sonxurxo.androidrunner.view.web.application.AndroidRunnerApplication;
import es.sonxurxo.androidrunner.view.web.components.game.notfinished.list.GameListPanel;
import es.sonxurxo.androidrunner.view.web.pages.BasePage;

/**
 * @author "Xurxo Mendez Perez"
 *
 */
public class FinishedGamesListPage extends BasePage {

	private static final int RESULTS_PER_PAGE = 5;
	
	public FinishedGamesListPage() {
		this(null);
	}
	
	public FinishedGamesListPage(PageParameters pageParameters) {
		super(pageParameters);
		
		this.add(new GameListPanel("finishedGamesPanel", new FinishedGameProvider(), FinishedGamesListPage.RESULTS_PER_PAGE));
		
	}
	
	class FinishedGameProvider implements IDataProvider  {
		
		private static final long serialVersionUID = -7749054103035343106L;
		
		private Integer resultsCount;
		
		public FinishedGameProvider() {
			super();
		}

		@SuppressWarnings("unchecked")
		public Iterator iterator(int startIndex, int count) {
			return AndroidRunnerApplication.get().getGameService()
				.findFinishedGames(startIndex, count).iterator();
		}

		public int size() {
			if (this.resultsCount == null) {
				this.resultsCount = AndroidRunnerApplication.get().getGameService()
				.countFinishedGames();
			}
			return this.resultsCount.intValue();
		}

		public IModel model(Object object) {
			return new FinishedGameModel((FinishedGameTO) object);
		}

		protected class FinishedGameModel extends LoadableDetachableModel {
			
			private static final long serialVersionUID = -5423144904529853027L;
			
			private Long id;

			public FinishedGameModel(FinishedGameTO finishedGameTO) {
				this.id = new Long(finishedGameTO.getGameId());
			}

			@Override
			protected Object load() {
				try {
					return AndroidRunnerApplication.get().getGameService().findFinishedGameById(this.id.longValue());
				} catch (InstanceNotFoundException e) {
					return null;
				}
			}
		}

		public void detach() {
			//Not needed            
		}
	}
	
	@Override
	protected String getTitle() {
		return getLocalizer().getString("finishedGamesList.title", FinishedGamesListPage.this);
	}
}
